﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SPORTident;

namespace sportident_sample
{
    public partial class Form1 : Form
    {
        SPORTident.Reader reader = new Reader();
        SPORTident.ReaderCardPunch readerCardPunch = new ReaderCardPunch();

        public Form1()
        {
            InitializeComponent();
            Start();
        }
        private void Start()
        {
            writeLog("Application started");
            reader.StationConfigRead += new StationConfigReadEventHandler(reader_StationConfigRead);
            reader.InputDeviceChanged += new DeviceChangedEventHandler(reader_InputDeviceChanged);
            reader.InputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader_InputDeviceStateChanged);
            reader.RadioReadoutCardIncomplete += new DataReadCompletedEventHandler(reader_RadioReadoutCardIncomplete);
            reader.RadioReadoutCardCompleted += new DataReadCompletedEventHandler(reader_RadioReadoutCardCompleted);
            reader.OnlineStampRead += new DataReadCompletedEventHandler(reader_OnlineStampRead);
            reader.CardRead += new DataReadCompletedEventHandler(reader_CardRead);
            reader.Log += new SPORTident.Common.FileLoggerEventHandler(reader_Log);
            reader.OutputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader_OutputDeviceStateChanged);
            reader.EventsQueryComplete += new EventsQueryCompleteEventHandler(reader_EventsQueryComplete);
            getSIDevices();
        }
        private void clearSIDevices()
        {
            writeLog("Clearing list of devices");
            comboBox_sportidentDevices.Items.Clear();
        }
        private void getSIDevices()
        {
            writeLog("Scanning for available devices");
            SPORTident.ReaderDeviceInfo.GetAvailableDeviceList();
            foreach (SPORTident.ReaderDeviceInfo device in SPORTident.ReaderDeviceInfo.AvailableDevices)
            {
                comboBox_sportidentDevices.Items.Add(device);
                writeLog("Adding device:"+device.DeviceName);
            }
            if (comboBox_sportidentDevices.Items.Count > 0)
            {
                writeLog("Found " + comboBox_sportidentDevices.Items.Count + " devices");
                comboBox_sportidentDevices.SelectedIndex = 0;
            }
            else
            {
                writeLog("No devices found");
            }
        }
        private void connect()
        {
            SPORTident.ReaderDeviceInfo readerDeviceInfo = (ReaderDeviceInfo)(comboBox_sportidentDevices.SelectedItem);
            if (radioButton_inputDevice.Checked==true)
            {
                reader.InputDevice = readerDeviceInfo;
                reader.OpenInputDevice();
            }
            else if (radioButton_outputDevice.Checked)
            {
                reader.OutputDevice = readerDeviceInfo;
                reader.OpenOutputDevice();
            }
            reader.BackupFileName = "c:\\si_test.txt";
            reader.WriteBackupFile = true;
        }
        private void disconnect()
        {
            if (radioButton_inputDevice.Checked)
            {
                reader.CloseInputDevice();
            }
            else if(radioButton_outputDevice.Checked)
            {
                reader.CloseOutputDevice();
            }
        }
        private void writeLog(string text)
        {
            listBox_log.Items.Add(text);
            listBox_log.Items.Add("(OutputBytes: " + reader.OutputBytes+", InputBytes: " + reader.InputBytes+", CardNumber: " + readerCardPunch.CardNumber+")");

            listBox_log.SelectedIndex = listBox_log.Items.Count - 1;
            listBox_log.SelectedIndex = -1;
        }
        #region reader event handlers
        void reader_OnlineStampRead(object sender, SportidentDataEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_RadioReadoutCardCompleted(object sender, SportidentDataEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_RadioReadoutCardIncomplete(object sender, SportidentDataEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_InputDeviceChanged(object sender, DeviceChangedEventArgs e)
        {
            writeLog("InputDeviceChanged: " + e.PreviousDevice + " => " + e.CurrentDevice);
            
        }
        void reader_CardRead(object sender, SportidentDataEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_InputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e)
        {
            writeLog("InputDeviceStateChanged: " + e.PreviousState + " => " + e.CurrentState);
        }
        void reader_EventsQueryComplete(object sender, EventsQueryCompleteEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_OutputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e)
        {
            throw new NotImplementedException();
        }
        void reader_StationConfigRead(object sender, StationConfigReadEventArgs e)
        {
            writeLog("StationConfigRead, OperatingMode: " + e.OperatingMode);
            writeLog("StationConfigRead, ExtendedProtocol: " + e.ExtendedProtocol);
            writeLog("StationConfigRead, AutoSend: " + e.AutoSend);

        }
        void reader_Log(object sender, SPORTident.Common.FileLoggerEventArgs e)
        {
            writeLog("Log: " + e.Message);
        }
        #endregion
        #region form controls event handlers
        private void button_sportidentConnect_Click(object sender, EventArgs e)
        {
            writeLog("Connect pressed");
            try
            {
                connect();
                comboBox_sportidentDevices.Enabled = false;
                button_refresh.Enabled = false;
                button_sportidentConnect.Enabled = false;
                button_sportidentDisconnect.Enabled = true;
                flowLayoutPanel1.Enabled = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button_sportidentDisconnect_Click(object sender, EventArgs e)
        {
            writeLog("Disconnect pressed");
            comboBox_sportidentDevices.Enabled = true;
            button_refresh.Enabled = true;
            button_sportidentConnect.Enabled = true;
            button_sportidentDisconnect.Enabled = false;
            flowLayoutPanel1.Enabled = true;
            disconnect();
        }
        private void button_refresh_Click(object sender, EventArgs e)
        {
            writeLog("Refresh pressed");
            clearSIDevices();
            getSIDevices();
        }
        #endregion
    }
}
